package flex.cc.nursing.api;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import flex.cc.common.utils.DateUtil;
import flex.cc.core.support.BaseController;
import flex.cc.nursing.service.AccountDataService;

/**
*
* @author WANGCONG
* 2022年3月23日上午9:25:16
* @desc 预发接口
*/
@RestController
@RequestMapping(value = "/storeapi/nursing")
public class AccountDataApiController extends BaseController{

	@Resource
    private AccountDataService accountDataService;
	
	/**
	*
	* @author WANGCONG
	* 2022年3月23日上午9:25:56
	* @desc 预发接口对接
	*/
	@ResponseBody
    @RequestMapping("/release")
    public JSONObject verify(HttpServletRequest request) throws Exception{
		JSONObject jsonResult = new JSONObject();
		String resultCode = "0000";
		String resultDesc = "成功";
		long startTime = System.currentTimeMillis();
    	logger.info("AccountDataApiController 预发接口:begintime="+DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
		try {
            request.setCharacterEncoding("UTF-8");// 设置编码
            // 读取请求报文
            String requestJson = IOUtils.toString(request.getInputStream(), "UTF-8");
            logger.info("AccountDataApiController 预发接口:reqJson="+requestJson);
            if (requestJson != null && !"".equals(requestJson)) {
            	JSONObject jsonObject = JSONObject.parseObject(requestJson);
            	String uniqueNo = (String)jsonObject.get("uniqueNo"); //车生活唯一标识
            	if(StringUtils.isBlank(uniqueNo)){
            		resultCode = "0001";
            		resultDesc = "请求报文参数uniqueNo不能为空";
                	logger.error("AccountDataApiController 预发接口请求报文参数uniqueNo不能为空");
            	}else{
            		accountDataService.save(jsonObject);
            	}
            }else {
            	resultCode = "0001";
            	resultDesc = "接口处理异常:请求报文为空";
            	logger.error("AccountDataApiController 预发接口处理异常:请求报文为空");
            }
		} catch (Exception e) {
			resultCode = "0001";
			resultDesc = "数据处理异常";
        	logger.error("AccountDataApiController 预发接口处理异常：" + e.getMessage());
		}
		jsonResult.put("resultCode", resultCode);
		jsonResult.put("resultDesc", resultDesc);
		long endTime = System.currentTimeMillis();
        logger.info("AccountDataApiController 预发接口:endtime="+DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS")+"; 处理时长="+(endTime - startTime)+"; resJson=?"+jsonResult.toString());
		return jsonResult;
	}
}
